/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaPrincipal.java
 *
 * Created on 28/03/2012, 22:30:02
 */

package tela;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import lixo.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Edu
 */
public class TelaPrincipal extends javax.swing.JFrame {
    public String usuario;

  
    
 
     /** Creates new form TelaPrincipal */
    public TelaPrincipal(String user) {
        this.usuario = user;
        initComponents();
        BarraDeStatus br = new BarraDeStatus();
        br.setVisible(true);
        alteraCursor(0);
        
       ImageIcon icon = new ImageIcon(this.getClass().getResource("/figura/icone.png"));    
       setIconImage(icon.getImage()); 
       
      try {  
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //Windows  
      }catch(Exception e) {  
         JOptionPane.showMessageDialog(null, e);
      }         
    }
    
    public void alteraCursor(int tipo) {
        
        if (tipo == 1) {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        }
         if (tipo == 0) {
        setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem21 = new javax.swing.JMenuItem();
        jScrollPane1 = new javax.swing.JScrollPane();
        jLabelUsuario = new javax.swing.JLabel();
        jLabelData = new javax.swing.JLabel();
        jLabelHoras = new javax.swing.JLabel();
        barraDeMenu = new javax.swing.JMenuBar();
        jMenuCadastro = new javax.swing.JMenu();
        jMenuItemPedidoSerivco = new javax.swing.JMenuItem();
        jMenuItemCadastroClientes = new javax.swing.JMenuItem();
        jMenuItem18 = new javax.swing.JMenuItem();
        jMenuItem17 = new javax.swing.JMenuItem();
        jMenuItem19 = new javax.swing.JMenuItem();
        jMenuItem20 = new javax.swing.JMenuItem();
        jMenuItem22 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        jMenuItemTesteDeTetrazolio = new javax.swing.JMenuItem();
        jMenuItemLivroDeProtocolo = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItemCadastrarFuncionario = new javax.swing.JMenuItem();
        jMenuAjuda = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        jMenuItem21.setText("jMenuItem21");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Softlabs");
        setBackground(new java.awt.Color(255, 255, 255));

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
        jScrollPane1.setBorder(null);
        jScrollPane1.setToolTipText("");
        jScrollPane1.setMaximumSize(null);
        jScrollPane1.setMinimumSize(null);
        jScrollPane1.setOpaque(false);

        jLabelUsuario.setBackground(new java.awt.Color(204, 204, 204));
        jLabelUsuario.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED, null, null, java.awt.Color.darkGray, java.awt.Color.darkGray));

        jLabelData.setBackground(new java.awt.Color(204, 204, 204));
        jLabelData.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED, null, null, java.awt.Color.darkGray, java.awt.Color.darkGray));

        jLabelHoras.setBackground(new java.awt.Color(204, 204, 204));
        jLabelHoras.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED, null, null, java.awt.Color.darkGray, java.awt.Color.darkGray));

        barraDeMenu.setBorder(null);
        barraDeMenu.setFont(new java.awt.Font("Segoe UI", 0, 14)); // NOI18N
        barraDeMenu.setOpaque(false);

        jMenuCadastro.setIcon(new javax.swing.ImageIcon(getClass().getResource("/figura/logo mini.png"))); // NOI18N
        jMenuCadastro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuCadastroActionPerformed(evt);
            }
        });

        jMenuItemPedidoSerivco.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItemPedidoSerivco.setText("Pedido de Serviço");
        jMenuItemPedidoSerivco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemPedidoSerivcoActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItemPedidoSerivco);

        jMenuItemCadastroClientes.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F2, 0));
        jMenuItemCadastroClientes.setText("Cliente");
        jMenuItemCadastroClientes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCadastroClientesActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItemCadastroClientes);

        jMenuItem18.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F3, 0));
        jMenuItem18.setText("Responsável Técnico");
        jMenuItem18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem18ActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItem18);

        jMenuItem17.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, 0));
        jMenuItem17.setText("Cultivares");
        jMenuItem17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem17ActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItem17);

        jMenuItem19.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F5, 0));
        jMenuItem19.setText("Informação de Resultado");
        jMenuItem19.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem19ActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItem19);

        jMenuItem20.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F6, 0));
        jMenuItem20.setText("Boletim");
        jMenuItem20.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem20ActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItem20);

        jMenuItem22.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F7, 0));
        jMenuItem22.setText("Certificador");
        jMenuItem22.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem22ActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItem22);
        jMenuCadastro.add(jSeparator2);

        jMenuItemTesteDeTetrazolio.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F8, 0));
        jMenuItemTesteDeTetrazolio.setText("Teste de Tetrazólio");
        jMenuItemTesteDeTetrazolio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemTesteDeTetrazolioActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItemTesteDeTetrazolio);

        jMenuItemLivroDeProtocolo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F9, 0));
        jMenuItemLivroDeProtocolo.setText("Livro de Protocolo");
        jMenuItemLivroDeProtocolo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemLivroDeProtocoloActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItemLivroDeProtocolo);
        jMenuCadastro.add(jSeparator1);

        jMenuItemCadastrarFuncionario.setText("Funcionário");
        jMenuItemCadastrarFuncionario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemCadastrarFuncionarioActionPerformed(evt);
            }
        });
        jMenuCadastro.add(jMenuItemCadastrarFuncionario);

        barraDeMenu.add(jMenuCadastro);

        jMenuAjuda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/figura/help.png"))); // NOI18N
        jMenuAjuda.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jMenuAjuda.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        jMenuItem1.setText("Sobre");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenuAjuda.add(jMenuItem1);

        barraDeMenu.add(jMenuAjuda);

        setJMenuBar(barraDeMenu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabelData, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelHoras, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabelHoras, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(jLabelData, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
       Sobre so = new Sobre();
       so.setLocationRelativeTo(null);
       so.setVisible(true);
       
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItemPedidoDeServicoActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItemPedidoDeServicoActionPerformed
       jScrollPane1.setViewportView(new CadastraBuscaPedidoDeServico());
    }//GEN-LAST:event_jMenuItemPedidoDeServicoActionPerformed

    private void jMenuItemLivroDeProtocoloActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItemLivroDeProtocoloActionPerformed
       jScrollPane1.setViewportView(new LivroDeProtocolo());
    }//GEN-LAST:event_jMenuItemLivroDeProtocoloActionPerformed

    private void jMenuItemTesteDeTetrazolioActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItemTesteDeTetrazolioActionPerformed
        jScrollPane1.setViewportView(new TesteDeTetrazolio());
    }//GEN-LAST:event_jMenuItemTesteDeTetrazolioActionPerformed

    private void jMenuItemCadastroClientesActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItemCadastroClientesActionPerformed
       jScrollPane1.setViewportView(new CadastraBuscaCliente());
    }//GEN-LAST:event_jMenuItemCadastroClientesActionPerformed

    private void jMenuItemCadastrarFuncionarioActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItemCadastrarFuncionarioActionPerformed
        if(TelaLogin.USUARIO_LOGADO.equals("admin")) {
            jScrollPane1.setViewportView(new CadastraBuscaFuncionario());
        } else {
            JOptionPane.showMessageDialog(null, "É preciso ser administrador do sistema para cadastrar um funcionário.", "Erro", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jMenuItemCadastrarFuncionarioActionPerformed

    private void jMenuItem17ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItem17ActionPerformed
    jScrollPane1.setViewportView(new CadastraBuscaCultivares());
    }//GEN-LAST:event_jMenuItem17ActionPerformed

    private void jMenuCadastroActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuCadastroActionPerformed

    }//GEN-LAST:event_jMenuCadastroActionPerformed

    private void jMenuItemPedidoSerivcoActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItemPedidoSerivcoActionPerformed
        jScrollPane1.setViewportView(new CadastraBuscaPedidoDeServico());
    }//GEN-LAST:event_jMenuItemPedidoSerivcoActionPerformed

    private void jMenuItem18ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jMenuItem18ActionPerformed
        jScrollPane1.setViewportView(new CadastraBuscaResponsavelTecnico());
  
    }//GEN-LAST:event_jMenuItem18ActionPerformed

    private void jMenuItem19ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem19ActionPerformed
       jScrollPane1.setViewportView(new CadastraBuscaInformacaoResultado());
    }//GEN-LAST:event_jMenuItem19ActionPerformed

    private void jMenuItem20ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem20ActionPerformed
        jScrollPane1.setViewportView(new CadastraBuscaBoletim());
    }//GEN-LAST:event_jMenuItem20ActionPerformed

    private void jMenuItem22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem22ActionPerformed
       jScrollPane1.setViewportView(new CadastraBuscaCertificador());
    }//GEN-LAST:event_jMenuItem22ActionPerformed

    /**
    * @param args the command line arguments
    */
   // public static void main(String args[]) {
        
    //    java.awt.EventQueue.invokeLater(new Runnable() {
      //      public void run() {
      //          new TelaPrincipal().setVisible(true);           
      //      }
      //  });
   // }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuBar barraDeMenu;
    private javax.swing.JLabel jLabelData;
    private javax.swing.JLabel jLabelHoras;
    private javax.swing.JLabel jLabelUsuario;
    private javax.swing.JMenu jMenuAjuda;
    private javax.swing.JMenu jMenuCadastro;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem17;
    private javax.swing.JMenuItem jMenuItem18;
    private javax.swing.JMenuItem jMenuItem19;
    private javax.swing.JMenuItem jMenuItem20;
    private javax.swing.JMenuItem jMenuItem21;
    private javax.swing.JMenuItem jMenuItem22;
    private javax.swing.JMenuItem jMenuItemCadastrarFuncionario;
    private javax.swing.JMenuItem jMenuItemCadastroClientes;
    private javax.swing.JMenuItem jMenuItemLivroDeProtocolo;
    private javax.swing.JMenuItem jMenuItemPedidoSerivco;
    private javax.swing.JMenuItem jMenuItemTesteDeTetrazolio;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    // End of variables declaration//GEN-END:variables

  
    public class BarraDeStatus extends JPanel {
    
    String diaSemana[] = {"Domingo", "Segunda-feira",
                              "Terça-feira", "Quarta-feira",
                              "Quinta-feira", "Sexta-feira",
                              "Sábado"};                
    String meses[] = {"Janeiro", "Fevereiro", "Março",
                          "Abril", "Maio", "Junho", "Julho",
                          "Agosto", "setembro", "Outubro",
                          "Novembro", "Dezembro"};
    
    public BarraDeStatus()
    {
        ActionListener tarefa = new ActionListener(){
      public void actionPerformed(ActionEvent e){
        horasData();
        usuario();
        
        
      }
    };
       javax.swing.Timer timer = new javax.swing.Timer(1000, tarefa);
       timer.start();
    }
    
    private void horasData()
    {
        Calendar agora = Calendar.getInstance();
        int ho = agora.get(Calendar.HOUR_OF_DAY);
        int mi = agora.get(Calendar.MINUTE);
        int ds = agora.get(Calendar.DAY_OF_WEEK);
        int dia = agora.get(Calendar.DAY_OF_MONTH);
        int mes = agora.get(Calendar.MONTH);
        int ano = agora.get(Calendar.YEAR);
    
        jLabelHoras.setText(" "+formatar(ho) + ":" + formatar(mi) +"");
        jLabelData.setText(" "+diaSemana[ds - 1] + ", " + formatar(dia) + " de " + meses[mes] + " de " + ano + "");
    }
    private void usuario() {
        jLabelUsuario.setText(" Usuário: "+ usuario);
    }
    private String formatar(int num){
    DecimalFormat df = new DecimalFormat("00");
    
    return df.format(num);
  }  
  } 
  public void geraRelatorioPedDetalhado(String senha) throws JRException, Exception {  
      
  //JasperPrint rel = null; 
        String driver = "org.postgresql.Driver";  
        String user   = "postgres";  
        String senhaa = "ewq321";  
        String url      = "jdbc:postgresql://localhost:5432/postgres";  
        //String teste = "dd";
               
                try 
                {
                    Class.forName(driver);
                } catch (ClassNotFoundException ex) 
                  {
                        Logger.getLogger(TelaLogin.class.getName()).log(Level.SEVERE, null, ex);
                  }
                
                Connection con = null;
                
                try 
                { 
                    con = (Connection) DriverManager.getConnection(url, user, senhaa);
                    java.sql.Statement stm = con.createStatement();
                    String sql = "select * from usuario where senha = '"+senha+"'";
                    ResultSet rs = stm.executeQuery(sql);
                    System.out.println("Conexão realizada com sucesso.");
            
        /* HashMap de parametros utilizados no relatório. Sempre instanciados */  
        Map parameters = new HashMap();  
        //Passa as datas como parâmetro para aparecer no relatório  
        parameters.put("senha", senha);//Aqui vc passa os parâmetros para um hashmap, que será enviado para o relatório  
            JRResultSetDataSource jrRS = new JRResultSetDataSource(rs);  
             
            /* Preenche o relatório com os dados. */  
            String jasperPrint = JasperFillManager.fillReportToFile("C:/Users/Eduardo/Documents/NetBeansProjects/Softlabs/src/relatorio/FichaAnaliseMilho.jasper", parameters, jrRS);//Aqui vc chama o relatório  
            JasperViewer viewer = new JasperViewer(jasperPrint, false, false);  
            viewer.setExtendedState(JasperViewer.MAXIMIZED_BOTH);//Coloca em maximizado  
            viewer.setTitle("Histórico - Tarefa nº " + senha);//Coloca um título no relatório  
            viewer.setVisible(true);  
        
           /* HashMap map = new HashMap();
            String arquivo = "relatorio/FichaAnaliseMilho.jasper";
            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo); 

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,map,con);

            JasperViewer jrviewer = new JasperViewer(jasperPrint, false);
            jrviewer.setVisible(true);
            */
          
            
        } catch (SQLException ex) {
            Logger.getLogger(TelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JRException ex) {
           Logger.getLogger(TelaPrincipal.class.getName()).log(Level.SEVERE, null, ex);
        
         }
        
    }  
   
}
    
   
    



    
    

